from vector import *
from rendering import *
from entityManager import *
import mapa
import math
import pygame.time
import resources
import sound
import sprinter
import gui
def AngleToTarget(pos,target):
     dx = target.get()[0] - pos.get()[0]
     dy = target.get()[1] - pos.get()[1]
     return -math.degrees(math.atan2(dy,dx))
class bullet:
    def __init__(self):
        pass
class Base:
    def __init__(self,surface,pos):
        self.surface = surface
        self.pos = pos
        self.body = r_object(surface,(0,255,0),40,40)
        self.text = text(surface,"BASE",(255,255,255))
        entityList.append(self)
        self.waveNumber = 0
    def draw(self):
        self.body.draw(self.pos)
        self.text.draw(self.pos)
    def think(self):
        if len(enemyList) == 0 and self.waveNumber != len(sprinter.wave) - 1 and len(sprinter.wave) != 0:
            for y in range(0,39):
                 sprinter.wave[self.waveNumber](vector(0,y*15),self.surface)
            self.waveNumber = self.waveNumber + 1
        if self.waveNumber == len(sprinter.wave) - 1 and len(enemyList) == 0:
            self.waveNumber = self.waveNumber - 1
        self.draw()
class powerPlant:
     def __init__(self,surface,pos):
         entityList.append(self)
         self.body = i_object("./Entities/graphics/Generator.png",surface)
         resources.Money = resources.Money - 50
         self.pos = pos
     def draw(self):
          self.body.draw(self.pos)
     def think(self):
          self.draw()
          resources.Energy = resources.Energy + 100
class tower_definition:
     def __init__(self,surface,pos,gunImage,gunBase):
          self.pos = pos
          self.surface = surface
          self.gunBase = i_obect(gunBase)
class tower:
    def __init__(self,surface,pos):
        self.pos = pos
        self.body = i_object("./Entities/graphics/tower_base.jpg",surface)
        resources.Money = resources.Money - 100
        sList = []
        self.surface = surface
        for i in range(1,10):
            sList.append("./Entities/graphics/tower/"+"tower ("+str(i)+").png")
        self.gun = sprite(sList,surface)
        self.gun.scale((40,40))
        del sList
        entityList.append(self)
        self.line = line(self.surface,(255,0,0),self.pos,vector(0,0))
        self.line.width = 1
        self.power = 5
        self.shooting = False
    def draw(self):
        self.gun.draw(self.pos)
    def shoot(self,pos):
        resources.Energy = resources.Energy - 10
        self.line.draw(vector(self.pos.get()[0]+20,self.pos.get()[1]+20),pos)
        self.shooting = True
    def upgrade(self):
        if resources.Money - 50 >= 0:
            resources.Money = resources.Money - 50
            self.power = self.power +5
    def sell(self):
        resources.Money = resources.Money + 50 + self.power/5*10
        buffer.remove(self)
        del self
    def think(self):
        if self.shooting:
            self.gun.setFrame(9)
        else:
            self.gun.setFrame(1)
        self.body.draw(self.pos)
        resources.Energy = resources.Energy - 1
        if len(enemyList) > 0:
            nearest = 0
            nearestDistance = 0
            for target in enemyList:
                distance = getDistance(self.pos,target.pos)
                if distance < nearestDistance and distance < 100 or nearestDistance == 0:
                    nearest = target
                    nearestDistance = distance
            if target != None:
                target = nearest
            if resources.Energy > 0:
                ang = AngleToTarget(self.pos,target.getPos())
                self.gun.setAngle(ang)
                self.shoot(target.getPos())
                target.hurt(self.power)
        else:
            self.shooting = False
        self.draw()
towerNames = ["Laser 100$","Generator 50$"]
towerPrices = [100,50]
towerEntities = [tower,powerPlant]
